// pages/category/index.js
import {
  request
} from "../../request/index.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //左侧分类数据
    leftMenuList: [],
    //右侧商品数据
    rightContent: [],
    //点击左侧分类的下标
    currentIndex: 0,
    //点击商品重新置顶  使scroll-view 重新置顶到顶部
    scrollTop: 0,
    //商品分类ID
    menuId:'',
  },
  QueryParams: {
    query: "",
    cid: "",
    currPage: 1,
    pageSize: 10
  },
  //全局page
  totalPages: 1,

  //接口的返回数值
  Cates: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCates();
  },

  //获取商品分类的标识
  menu_onclick(e) {
   let menuId = e.currentTarget.dataset.item;
   let rightContent =[]
   this.setData({
    menuId,
    rightContent
   })
   this.getRightListData();
  },

  getRightListData(){
    request({
        url: "/list",
        data: {
          menuId: this.data.menuId,
          currPage: this.QueryParams.currPage,
          pageSize: this.QueryParams.pageSize
        }
      }).then(result => {
        this.Cates = result;
        //构造右侧的商品数据
        this.totalPages = this.Cates.data.data.totalCount;
        console.log('页数===============00000000'+JSON.stringify(this.Cates.data.data.list))
        this.setData({
          // scrollTop: 0,
          //拼接数组  原数组+新数组
          rightContent: this.data.rightContent.concat(this.Cates.data.data.list)
        })
      })
  },
  //获取分类数据
  getCates() {
    request({
      url: "/menuList",
    }).then(result => {
      this.Cates = result;
      //构造左侧分类数据
      let leftMenuList = this.Cates.data;
      let menuId = leftMenuList.data[0].menuId;
      console.log(menuId)
      this.setData({
        leftMenuList,
        menuId
      })
      request({
        url: "/list",
        data: {
          menuId: menuId,
          currPage: this.QueryParams.currPage,
          pageSize: this.QueryParams.pageSize
        }
      }).then(result => {
        this.Cates = result;
        let rightContent = this.Cates.data.data.list;
        this.totalPages = this.Cates.data.totalCount;
        console.log('页数====================================='+this.Cates.data)
        console.log('页数====================================='+this.Cates.data.totalPage)
        
        //构造右侧的商品数据
        this.setData({
          rightContent
        })
      })
    })
  },
  //跳转
  onclickDetails: function (e) {
    console.log(e.currentTarget.dataset.text)
    let details = e.currentTarget.dataset.text;
    wx.navigateTo({
      url: '/pages/details/index',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('details', {
          data: details
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    //判断时候存在下一页数据 如果当前页数大于等于 计算的总页数 怎没有下一页数据
    if (this.QueryParams.currPage >= this.totalPage) {
      //没有下一页数据
      wx.showToast({
        title: '暂无下页数据',
      });
    } else {
      //还有下一页数据
      this.QueryParams.currPage++;
      this.getRightListData();
    }

  },
})